using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Dimebrain.TweetSharp.Core.Extensions;
using twimilar.DomainModel;

namespace twimilar
{
	public static class Extensions
	{
		public static string ToFileName(this string name)
		{
			var exceptionsList = new List<char> { '/', '|', '\\', '"', ':', '?' };
			foreach (char symb in exceptionsList)
			{
				name = Regex.Replace(name, @"\" + symb, string.Empty);
			}
			return name;
		}

		public static List<string> GetTop(this IDictionary<string, int> dictionary, int count)
		{
			var sorted = from key in dictionary.Keys
									 orderby dictionary[key] descending
									 select key;

			return sorted.Take(count).ToList();
		}

		public static IEnumerable<string> GetWords(this string message)
		{
			return message.Split(' ')
				.ToList()
				.Map(x => x.Trim().ToLowerInvariant())
				.Filter(x => (x.Count() > 3) && (!x.Contains(@"@")));
		}

		public static string ReplaceUriWithAt(this string item)
		{
			return Regex.Replace(item, "http://twitter.com/", string.Empty);
		}

		public static List<KeyValuePair<User, int>> GetTop(this Dictionary<User, int> dictionary, int count)
		{
			var sorted = from item in dictionary
									 orderby item.Value descending
									 select item;

			return sorted.Take(count).ToList();
		}
	}
}